home *** CD-ROM | disk | FTP | other *** search
- // eHelp« Corporation
- // Copyright⌐ 1998-2001 eHelp« Corporation.All rights reserved.
- // RoboHelp_CSH.java
- // The Helper class for RoboInfo and WebHelp Context Sensitive Help
-
- /* Example function calls
- // First, create RH_WindowOption object
- RoboHelp_CSH.RH_WindowOption MyWin = new RoboHelp_CSH.RH_WindowOption();
- // Set window style and position in object
- MyWin.m_nStyle = RoboHelp_CSH.RHWO_LOCATION | RoboHelp_CSH.RHWO_MENUBAR | RoboHelp_CSH.RHWO_RESIZABLE;
- MyWin.m_nTop = 50;
- MyWin.m_nLeft=50;
- MyWin.m_nHeight=300;
- MyWin.m_nWidth=400;
-
- // Show Help for topic with HTML Help Map number 11:
- RoboHelp_CSH.RH_ShowHelpById("c:\\myapp\\help\\start_rhc.htm", "11", "myHelpWindow", MyWin)
-
- // Show Help for topic with Context String ID "My_Topic"
- RoboHelp_CSH.RH_ShowHelpByString("c:\\myapp\\help\\start_rhc.htm", "My_Topic", "myHelpWindow", MyWin)
- */
-
- import netscape.javascript.*;
- import java.applet.Applet;
- import java.net.*;
- import java.io.File;
-
-
- public class RoboHelp_CSH
- {
- public static class RH_WindowOption {
- public RH_WindowOption() {};
- public int m_nStyle; /* see above RHWO_XXX */
- public int m_nTop; /* top position (Y) */
- public int m_nLeft; /* left position (X) */
- public int m_nHeight; /* window's height */
- public int m_nWidth; /* window's width */
- };
- public static final int RHWO_LOCATION = 0x1;
- public static final int RHWO_MENUBAR = 0x2;
- public static final int RHWO_RESIZABLE = 0x4;
- public static final int RHWO_TOOLBAR = 0x8;
- public static final int RHWO_STATUS = 0x10;
- public static final int RHWO_SCROLLBARS = 0x20;
- private static final String AUTOCONTEXTID_PREFIX = "HelpIdFromHTMLHelp";
-
- /**********************************************
- Show Help topic by topic number generated for HtmlHelp.
- Parameters:
- [in] strUrlToHelpSet: url to helpset. for WebHelp Enterprise it is the url to server name, such as "http://helpserver.com/RoboApi.asp"
- for webhelp, it is full path name the webhelp start page name, such as "c:/myapp/help/start_rhc.htm" or "/user/myapp/help/start_rhc.htm"
- [in] nTopicNumber: topic number generated for the HTMLHelp context sensitive help.
- [in] strTargetId: browser's target name, such as "mynewtopic"
- [in] cWindowOption: browser options. can be 0.
- Result:
- none.
- **********************************************/
-
- public static boolean RH_ShowHelpById(String strUrlToHelpSet, int nTopicNumber, String strTargetId, RH_WindowOption cWindowOption, Applet applet)
- {
- String strContextID = AUTOCONTEXTID_PREFIX + "_" + nTopicNumber;
- return RH_ShowHelpByString(strUrlToHelpSet, strContextID, strTargetId, cWindowOption, applet);
- }
-
- /**********************************************
- Show Help topic
- Parameters:
- [in] strUrlToHelpSet: url to helpset. for WebHelp Enterprise it is the url to server name, such as "http://helpserver.com/RoboApi.asp"
- for webhelp, it is full path name the webhelp start page name, such as "c:/myapp/help/start_rhc.htm" or "/user/myapp/help/start_rhc.htm"
- [in] strContextID: context id of the topic
- [in] strTargetId: browser's target name, such as "mynewtopic"
- [in] cWindowOption: browser options. can be 0.
- Result:
- none.
- **********************************************/
- public static boolean RH_ShowHelpByString(String strUrlToHelpSet, String strContextID, String strTargetId, RH_WindowOption cWindowOption, Applet applet)
- {
- if (strUrlToHelpSet == null || strUrlToHelpSet.length() == 0 ) return false;
- if (cWindowOption == null)
- {
- cWindowOption = new RH_WindowOption();
- cWindowOption.m_nStyle = 0;
- cWindowOption.m_nTop = 10;
- cWindowOption.m_nLeft = 10;
- cWindowOption.m_nHeight = 300;
- cWindowOption.m_nWidth = 400;
- }
-
- String strURL = "";
- if (strContextID == null)
- strContextID = "";
-
- //let different agent to make url.
- if (isServerBased(strUrlToHelpSet))
- {
- strURL = makeUrlForServerBased(strUrlToHelpSet,strContextID);
- }
- else
- {
- strURL = makeUrlForWebHelpBased(strUrlToHelpSet,strContextID);
- }
- showHelpTopic(strURL, cWindowOption, strTargetId, applet);
- return true;
- }
-
- public static boolean isServerBased(String strUrlToHelpSet)
- {
- return (strUrlToHelpSet.toLowerCase().lastIndexOf(".asp") ==
- strUrlToHelpSet.length() - 4);
- }
-
- public static String makeUrlForServerBased(String strUrlToHelpSet, String strContextID)
- {
- return strUrlToHelpSet + "?context=" + strContextID;
- }
-
- public static String makeUrlForWebHelpBased(String strUrlToHelpSet, String strContextID)
- {
- return strUrlToHelpSet + "#context=" + strContextID;
- }
-
- private static boolean showHelpTopic(String strURL, RH_WindowOption cWindowOption, String strTargetId, Applet applet) {
- String strHelpOptions = "";
-
- if ((cWindowOption.m_nStyle & RHWO_LOCATION) != 0) // Show/Hide Address bar {yes | no}
- strHelpOptions += ",location=yes";
- else
- strHelpOptions += ",location=no";
-
- if ((cWindowOption.m_nStyle & RHWO_TOOLBAR) != 0) // Show/Hide Toolbar {yes | no}
- strHelpOptions += ",toolbar=yes";
- else
- strHelpOptions += ",toolbar=no";
-
- if ((cWindowOption.m_nStyle & RHWO_MENUBAR) != 0) // Show/Hide Menubar {yes | no}
- strHelpOptions += ",menubar=yes";
- else
- strHelpOptions += ",menubar=no";
-
- if ((cWindowOption.m_nStyle & RHWO_STATUS) != 0) // Show/Hide Statusbar {yes | no}
- strHelpOptions += ",status=yes";
- else
- strHelpOptions += ",status=no";
-
- if ((cWindowOption.m_nStyle & RHWO_SCROLLBARS)!= 0) // Show/Hide Scrollbar {yes | no}
- strHelpOptions += ",scrollbars=yes";
- else
- strHelpOptions += ",scrollbars=no";
-
- if ((cWindowOption.m_nStyle & RHWO_RESIZABLE)!= 0) // Allow help window resizing {yes | no}
- strHelpOptions += ",resizable=yes";
- else
- strHelpOptions += ",resizable=no";
-
- if (cWindowOption.m_nTop >= 0)
- {
- strHelpOptions += ",top=" + cWindowOption.m_nTop; // Top position for help window (in pixels)
- strHelpOptions += ",screenY=" + cWindowOption.m_nTop; // Top position for help window (in pixels)(for netscape)
- }
-
- if (cWindowOption.m_nLeft >= 0)
- {
- strHelpOptions += ",left=" + cWindowOption.m_nLeft; // Left position for help window (in pixels)
- strHelpOptions += ",screenX=" + cWindowOption.m_nLeft; // Left position for help window (in pixels)(for netscape)
- }
-
- if (cWindowOption.m_nWidth > 0)
- {
- strHelpOptions += ",width=" + cWindowOption.m_nWidth; // Width of help window (in pixels)
- strHelpOptions += ",outerWidth=" + cWindowOption.m_nWidth; // Width of help window (in pixels) (for netscape)
- }
-
- if (cWindowOption.m_nHeight > 0)
- {
- strHelpOptions += ",height=" + cWindowOption.m_nHeight; // Height of help window (in pixels)
- strHelpOptions += ",outerHeight=" + cWindowOption.m_nHeight; // Height of help window (in pixels) (for netscape)
- }
-
- boolean bJSAccessable = true;
- boolean bSuccess = false;
- if (System.getProperty("os.name").startsWith("Mac") && !System.getProperty("java.vendor").startsWith("Netscape"))
- bJSAccessable = false;
-
- if (bJSAccessable) {
- bSuccess = contextUsingJSObject(applet, strHelpOptions, strURL);
- }
-
- if (!bJSAccessable || !bSuccess) {
- try {
- String strURLWithWindowOption = strURL;
- URL url = makeURL(applet.getDocumentBase(), strURL, strURL);
- applet.getAppletContext().showDocument(url, "HelpStub");
- return true;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- return false;
- }
-
- private static boolean contextUsingJSObject(Applet applet, String strWindowOption, String strURLWithTopic)
- {
- try {
- URL url = makeURL(applet.getDocumentBase(),strURLWithTopic, strURLWithTopic);
- JSObject win = JSObject.getWindow(applet);
- String strURLConvert = url.toString();
- strURLConvert = strURLConvert.replace('\\', '/');
- win.eval("window.open(\"" + strURLConvert + "\", \"HelpStub\", \"" + strWindowOption + "\");");
- return true;
- }
- catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
-
- // return a URL build from the local and url parameters
- static URL makeURL (URL base, String local, String url) throws MalformedURLException
- {
- try {
- //return new URL (base, local);
- String protocol = base.getProtocol();
- String host = base.getHost();
- String file = base.getFile();
- int port = base.getPort();
-
- String fileNew0 = tuHtmlToText(file);
- String fileNew1 = GetNormalizedLocal(fileNew0);
- String fileNew = TruncURLtoQuestionMark(fileNew1);
- URL baseNew = new URL(protocol,host,port,fileNew);
-
- String localNew0 = tuHtmlToText(local);
- String localNew = GetNormalizedLocal(localNew0);
- return new URL (baseNew, localNew);
- }
- catch (MalformedURLException x) {
- x.printStackTrace();
-
- // no it isn't - but it might be
- // a MS-type file spec (eg e:\temp\foo)
- // so try opening it as a file
-
- File f = new File (local);
- if (f.exists ()) {
- // OK - it's a local file, try appending
- // "file:/" onto it and opening it - even
- // though the slashes will be all funny
- // this seems to work!
- return new URL ("file:/" + local);
- }
- else {
-
- // the file doesn't exist....
- // should display a dialog box here
- // and now try the secondary URL
-
- return new URL (base, url);
- }
- }
- }
-
- static String TruncURLtoQuestionMark(String str)
- {
- String strRc = str;
- int nQuestionMarkPos = str.indexOf('?');
- if (nQuestionMarkPos != -1) {
- strRc = str.substring(0, nQuestionMarkPos);
- }
- return strRc;
- }
-
- static String GetNormalizedLocal(String str)
- {
- String local = str;
- // fix up special characters (for netscape)
- for (int i = 0; i < local.length(); i++)
- {
- if (local.charAt(i) > 127)
- {
- local = local.substring(0, i) + "%" + Integer.toString(local.charAt(i), 16) + local.substring(i + 1, local.length());
- }
- }
- return local;
- }
-
- //To Do: need a utility to convert html<->text
- //Copy from SiteMapParserToContents
- //String fixSpecialCharacters(String value)
- static String tuHtmlToText(String value)
- {
- if (value == null)
- return null;
-
- // TODO: This should be common in SiteMapParser or something
- int i = value.indexOf('&');
-
- if (i < 0) return value;
-
- String strOut = "";
-
- // have to convert & and the like
- while (i > -1 && i < value.length() - 2) {
- strOut += value.substring(0, i);
- // System.out.println("1strOut is now '" + strOut + "'");
- String str = value.substring(i);
- String strEnd = "";
- int j = str.indexOf(';');
- if (j < 0) {
- strOut += str;
- // System.out.println("2strOut is now '" + strOut + "'");
- break; // no termination
- }
- if (j < str.length()-1) {
- value = str.substring(j+1);
- }
- else {
- value = "";
- }
- str = str.substring(1, j);
- // System.out.println("value is now '" + value + "'");
- // System.out.println("str is now '" + str + "'");
- // TODO: Should use a hashtable and support more characters
- switch (Character.toUpperCase(str.charAt(0)))
- {
- case 'A':
- if (str.equalsIgnoreCase("amp")) {
- str = "&";
- }
- break;
- case 'C':
- if (str.equalsIgnoreCase("copy")) {
- str = "(c)";
- }
- break;
- case 'G':
- if (str.equalsIgnoreCase("gt")) {
- str = ">";
- }
- break;
- case 'L':
- if (str.equalsIgnoreCase("lt")) {
- str = "<";
- }
- break;
- case 'N':
- if (str.equalsIgnoreCase("nbsp")) {
- str = " ";
- }
- break;
- case 'Q':
- if (str.equalsIgnoreCase("quot")) {
- str = "\"";
- }
- break;
- case 'R':
- if (str.equalsIgnoreCase("reg")) {
- str = "(R)";
- }
- break;
- }
- strOut += str;
- i = value.indexOf('&');
- if (i < 0) {
- strOut += value;
- // System.out.println("3strOut is now '" + strOut + "'");
- }
- }
-
- // System.out.println("4strOut is now '" + strOut + "'");
- return strOut;
- }
- }
-